package com.citycloud.ccuap.tc.sync.aspect;

import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.sys.entity.SysProject;
import com.citycloud.ccuap.commons.sys.entity.SysUser;
import com.citycloud.ccuap.commons.sys.project.IProjectService;
import com.citycloud.ccuap.tc.sync.dto.DeletedDeptUserDTO;
import com.citycloud.ccuap.tc.sync.dto.UserDeptRelationDTO;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncOperation;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncType;
import com.citycloud.ccuap.tc.sync.dto.SyncDataDto;
import com.citycloud.ccuap.tc.sync.service.IMessageService;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zjl
 * @date 2020/04/30 9:58
 */
@Aspect
@Component
public class UserDeleteAspect {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IProjectService projectService;

    @Pointcut("execution(* com.citycloud.ccuap.commons.sys.user.UserController.delete())||"
            + "execution(* com.citycloud.ccuap.commons.sys.user.UserController.deleteList())")
    public void delete() {
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @AfterReturning(value = "delete()", returning = "key")
    public void delete(JoinPoint joinPoint, Object key) throws Throwable {
        if (key instanceof JSONData) {
            JSONData jsonData = (JSONData) key;
            Map<String, Object> data = (Map) jsonData.getData();
            SysUser deleteUser = ((SysUser) data.get("user"));
            List<SysUser> userList = ((List<SysUser>) data.get("userList"));
            Long deptId = (Long) data.get("deptId");
            Long projectId = (Long) data.get("projectId");

            ArrayList<SysUser> lists = new ArrayList<>();
            if (deleteUser != null) {
                lists.add(deleteUser);
            }
            if (userList != null) {
                lists.addAll(userList);
            }
            // MQ 推送站点用户删除
            sendDeletedJmsMessage(lists, deptId, projectId);
        }
    }

    /**
     * 删除用户信息发送MQ消息
     *
     * @param userList
     * @param userDeptId
     * @param projectId
     */
    private void sendDeletedJmsMessage(List<SysUser> userList, Long userDeptId, Long projectId) {
        // 平台操作删除部门前，各站点已删除导入的对应部门，此处不用考虑
        if (projectId != null && CollectionUtils.isNotEmpty(userList)) {
            SysProject project = projectService.findById(projectId);
            if (project != null && project.getMqOff()) {
                List<UserDeptRelationDTO> userDepts = new ArrayList<>();
                userList.forEach(user -> {
                    UserDeptRelationDTO userDept = new UserDeptRelationDTO();
                    userDept.setDeptId(userDeptId == null ? user.getDeptId() : userDeptId);
                    userDept.setUserId(user.getUserId());
                    userDepts.add(userDept);
                });

                DeletedDeptUserDTO dto = new DeletedDeptUserDTO();
                dto.setUserDepts(userDepts);
                SyncDataDto<DeletedDeptUserDTO> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_USER,
                        GlobalSyncOperation.DELETED, projectId, dto);
                messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_USER_DEPT);
            }
        }
    }

}
